<?php

/**
 * @author Cesar Augustus Silva <cesaraugustussilva@linuxmail.org>
 * @copyright Copyright (c) 2013, Cesar Augustus Silva
 * @license http://www.gnu.org/licenses/gpl.html GNU GPL v3.0
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <­http://www.gnu.org/licenses/>.
 */

if ($_POST) {
    require_once(dirname(__FILE__).'/../config/config.inc.php');
    
    if (!isset($_POST['id'])) {
        if (isset($_POST['cpf'])) {
            $data = array(
                'cpf' => preg_replace('/\D/s', '', $_POST['cpf']),
                'name' => $_POST['name'],
                'id_city' => $_POST['city'],
                'cep' => preg_replace('/\D/s', '', $_POST['cep']),
                'address' => $_POST['address'],
                'number' => $_POST['number'],
                'district' => $_POST['district'],
                'complement' => $_POST['complement'],
                'contacts' => $_POST['contacts']
            );
            $id = DbSet::customers($data);
        }
        else {
            $data = array(
                'id_status' => 1,
                'cnpj' => preg_replace('/\D/s', '', $_POST['cnpj']),
                'fancyname' => $_POST['fancyname'],
                'corporatename' => $_POST['corporatename'],
                'id_city' => $_POST['city'],
                'cep' => preg_replace('/\D/s', '', $_POST['cep']),
                'address' => $_POST['address'],
                'number' => $_POST['number'],
                'district' => $_POST['district'],
                'complement' => $_POST['complement'],
                'contacts' => $_POST['contacts'],
                'website' => preg_replace('/^http:\/\//s', '', $_POST['website'])
            );
            $id = DbSet::enterprises($data);
        }

        for ($i = 0; $i < count($_POST['phone-number']) ; $i++)
            if (!empty($_POST['phone-number'][$i])) {
                $data = array(
                    'id_type' => $_POST['phone-type'][$i],
                    'areacode' => $_POST['phone-areacode'][$i],
                    'number' => ereg_replace('-', '', $_POST['phone-number'][$i])
                );
                if (isset($_POST['cpf']))
                    DbSet::customers_phones($id, $data);
                else
                    DbSet::enterprises_phones($id, $data);
            }

        $data = array(
            'id' => $id,
            'active' => 0,
            'email' => $_POST['email'],
            'password' => md5($_POST['password'])
        );
        if (isset($_POST['cpf']))
            $data['type'] = 'F';
        else
            $data['type'] = 'J';
        DbSet::accounts($data);
    }
    else
        $id = $_POST['id'];
    
    $addr = $_POST['email'];
    if (isset($_POST['cnpj']))
        $name = $_POST['fancyname'];
    else
        $name = $_POST['name'];
    $subject = 'Ativação - Goiautos';

    $message = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'."\n";
    $message .= '<HTML>'."\n";
    $message .= '    <HEAD>'."\n";
    $message .= '        <META HTTP-EQUIV=\"CONTENT-TYPE\" CONTENT=\"text/html; charset=utf-8\">'."\n";
    $message .= '    </HEAD>'."\n";
    $message .= '    <BODY LANG=\"pt-BR\" DIR=\"LTR\">'."\n";
    $message .= '        <p>Olá '.$name.'</p>'."\n";
    $message .= '        <p>Você acaba de se cadastrar em nossa <b>área para clientes</b>.</p>'."\n";
    $message .= '        <p>Nesta área você pode cadastrar veículos, bem como acompanhar estatísticas de visitação de seus veículos, e ter acesso exclusivo a ferramentas e serviços exclusivos para usuários cadastrados.</p>'."\n";
    $message .= '        <p><b>Para ativar seu cadastro clique no link abaixo:</b></p>'."\n";
    $message .= '        <p><a href="http://'.$_SERVER['HTTP_HOST'].__BASE_URI__.'register?activate='.md5($id).'&type='.(isset($_POST['cnpj']) ? md5('J') : md5('F')).'" target="_blank">'."\n";
    $message .= '            http://'.$_SERVER['HTTP_HOST'].__BASE_URI__.'register?activate='.md5($id).'&type='.(isset($_POST['cnpj']) ? md5('J') : md5('F')).'</p>'."\n";
    $message .= '        </a></p>'."\n";
    $message .= '        <p>Após confirmar seu cadastro, você será automaticamente redirecionado a área exclusiva para clientes <b>Goiautos</b>.</p>'."\n";
    $message .= '        <br/>'."\n";
    $message .= '        <p>Caso tenha qualquer dúvida, favor entrar em contato conosco através de nossa central de atendimento ao cliente <a href="http://'.$_SERVER['HTTP_HOST'].__BASE_URI__.'contato" target="_blank">clicando aqui</a>.</p>'."\n";
    $message .= '    </BODY>'."\n";
    $message .= '</HTML>'."\n";
    
    $email_control = DbGet::miscellaneous('email_control');
    $headers = 'MIME-Version: 1.1'."\n";
    $headers .= 'Content-type: text/html; charset=utf-8'."\n";
    $headers .= 'Content-Transfer-Encoding: 8bit'."\n";
    $headers .= 'From: "Goiautos" <'.$email_control.'>'."\n";
    $headers .= 'Reply-To: "Goiautos" <'.$email_control.'>'."\n";
    $headers .= 'Return-Path: '.$email_control."\n";

    if (mail($addr,$subject,$message,$headers))
        echo 'OK';
    else
        echo 'ERROR';
}